#!/usr/bin/env python
# Author:XXX


import os
import random
import synonyms

print("start")

def get_file_content(filename, mode):
    try:
        if mode == "knowledge":
            with open(filename, "r") as f:
                lines = f.readlines()
            return lines
        elif mode == "test":
            with open(filename, "r", encoding='UTF-8-sig') as f:
                department = f.readline()
                project_title = f.readline()
                no_info = f.readline()
                lines = f.readlines()
            return department, project_title, lines
        else:
            print("please input the correct mode parameter, such as 'knowledge' or 'test'")
    except IOError:
        print("File is not found or File is not accessible.")

def generate_knowledge(filename):
    lines = get_file_content(filename, "knowledge")
    know_dict = {}
    for line in lines:
        tmp = line.split(" ")
        key = tmp[0]
        value = tmp[1].replace("\n", "")
        #print("%s %s" % (key,value))
        if key in know_dict:
            previous_value = know_dict[key]
            #print(type(previous_value))
            previous_value.append(value)
            know_dict[key] = previous_value
        else:
            tmplist = []
            #print(know_dict)
            tmplist.append(value)
            know_dict[key] = tmplist
    return know_dict

def generate_test_wordlist(filename, match_list):
    department, project_title, test_lines = get_file_content(test_filename, "test")
    wordlist = []
    for line in test_lines:
        tmp = line.split("\t")
        col_1 = tmp[0]
        col_2 = tmp[1]
        if col_2 in match_list:
            wordlist.append(col_1)
    return department.replace("\n",""), project_title.replace("\n",""), wordlist

def get_similar_words(knowledge_wordlist, test_wordlist, threshold):
    if len(knowledge_wordlist) == 0:
        print("The department has no knowledge in our database!!!")
        return []
    else:
        top_similar_words = []
        for t in test_wordlist:
            for k in knowledge_wordlist:
                # TODO Calculating similarity
                #simi = random.uniform(10, 20)
                #simi=19
                #synonyms.compare("你好","您好", seg=True)
                simi = synonyms.compare(t, k, seg=True)
                if simi > threshold:
                    top_similar_words.append(t)
                    break
        return top_similar_words

if __name__ == "__main__":
    #####  Knowledge ####
    knowledge_filename = "./data/db/knowledge.txt"
    know_dict = generate_knowledge(knowledge_filename)
    print(know_dict)

    #####  Test Word List #####
    test_filename = "./data/test_db/word1.txt"
    match_list = ["n", "vn"]
    department, project_title, test_wordlist = generate_test_wordlist(test_filename, match_list)
    print(department)
    print(project_title)
    print(test_wordlist)
   
    ##### Calculate Similarity #####
    threshold = 0.5
    knowledge_wordlist = know_dict[department]
    top_similar_words = get_similar_words(knowledge_wordlist, test_wordlist, threshold)
    print(top_similar_words)
